package com.tbvtc.agriculturalcreditchainserver2025.utils.http;

import org.springframework.web.reactive.function.client.WebClient;
import reactor.core.publisher.Flux;

public class ReactiveClient {

    public static void main(String[] args) {
        // 创建 WebClient
        WebClient client = WebClient.create("http://localhost:8080");

//        // 发送 GET 请求，并接收 Flux<String>
//        Flux<String> responseFlux = client.get()
//                .uri("/ai/chat?message=你好")
//                .retrieve()
//                .bodyToFlux(String.class);
        Flux<String> responseFlux = client.get()
                .uri(uriBuilder -> uriBuilder
                        .path("/ai/chat")
                        .queryParam("prompt", "你好")
                        .build())
                .retrieve()
                .bodyToFlux(String.class);



        // 订阅并处理流数据（非阻塞）
        responseFlux.subscribe(
            data -> System.out.println("Received: " + data), // 处理每条数据
            error -> System.err.println("Error: " + error),  // 处理错误
            () -> System.out.println("Stream completed")     // 流完成后的回调
        );

        // 防止主线程退出（如果是简单测试）
        try {
            Thread.sleep(5000); // 等待流数据处理
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}